﻿Imports WeifenLuo.WinFormsUI.Docking
Imports System.ComponentModel

Public Class DownloadWallpaperForm
  Inherits DockContent

  Private _dw() As DownloadabeWallpaper
  Private _service_manager As ServiceManager = ServiceManager.GetServiceManager
  Private _config As Configuration = Configuration.GetObject

  Private _bgworkers As New List(Of BackgroundWorker)

  Private Sub DownloadWallpaperForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TabText = "Neue Bilder Herunterladen"
    Me.StatusLabel.Text = ""
    
    For Each c As Control In MyBase.Controls
  		If TypeOf c Is System.Windows.Forms.TableLayoutPanel Then
  			DirectCast(c, System.Windows.Forms.TableLayoutPanel).Dock = DockStyle.Fill
  		End If
  	Next
  	
  End Sub

  Private Sub BaseSizeChangedEvent(sender As Object, e As EventArgs) Handles MyBase.SizeChanged 
    Me.ListView1.Height = MyBase.Height - 50
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Cursor = Cursors.WaitCursor
    ListView1.Items.Clear()
    StatusLabel.Text = "Prüfe RSS..."
    Refresh()
    _dw = _service_manager.GetWallpaperService.GetListOfNew(_config.RSSUrl)
    If _dw.Length > 0 Then
      ProgressBar1.Maximum = _dw.Length + 1
      ProgressBar1.Value = 1

      _bgworkers.Clear()

      For i As Integer = 0 To _dw.Length - 1 Step 1
        Dim args As New ArrayList
        _bgworkers.Add(New BackgroundWorker)
        AddHandler _bgworkers(i).DoWork, AddressOf BgWorkerDownload
        AddHandler _bgworkers(i).ProgressChanged, AddressOf BgWorkerProgressChanged
        Dim lvi As ListViewItem = ListView1.Items.Add("Downloading...")
        lvi.SubItems.Add("...")
        lvi.SubItems.Add(_dw(i).FullName)
        lvi.SubItems.Add(_dw(i).Filename)
        lvi.SubItems.Add(_dw(i).Resolution)
        lvi.SubItems.Add(_dw(i).URL)
        _bgworkers(i).WorkerReportsProgress = True
        args.Add(_dw(i))
        args.Add(i)
        _bgworkers(i).RunWorkerAsync(args)
      Next

      StatusLabel.Text = ""
    Else
      StatusLabel.Text = "Keine neuen Wallapaper für die Auflösung " & _config.Resolution & " gefunden"
    End If
    Cursor = Cursors.Default
  End Sub


  Private Sub BgWorkerDownload(ByVal sender As Object, ByVal e As DoWorkEventArgs)

    'Threading.Thread.Sleep(200)
    DirectCast(DirectCast(e.Argument, ArrayList)(0), DownloadabeWallpaper).DownloadFile()
    _bgworkers(DirectCast(DirectCast(e.Argument, ArrayList)(1), Integer)).ReportProgress(1, e.Argument)
    

  End Sub
  
  Private Sub BgWorkerProgressChanged(ByVal sender As Object, _
  ByVal e As System.ComponentModel.ProgressChangedEventArgs)

    Dim w As DownloadabeWallpaper = DirectCast(DirectCast(e.UserState, ArrayList)(0), DownloadabeWallpaper)
    'Dim lviindex As Integer 

    Dim dw As DownloadabeWallpaper = w
    ListView1.Items(DirectCast(DirectCast(e.userstate, ArrayList)(1), Integer)).Text = "Fertig"
    Refresh()
    ProgressBar1.Value += 1

  End Sub

  Private Sub DownloadWallpaperForm_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
    MdiMain.Text = "Wallpaper Manager - " & Me.TabText
  End Sub

  Private Sub DownloadWallpaperForm_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
    MdiMain.Text = "Wallpaper Manager"
  End Sub
  
  Private Sub FormClosing_Eventhandler(sender As Object, e As FormClosingEventArgs) handles mybase.FormClosing 
  	e.Cancel = true 
  End Sub
  
End Class